﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RATSharp.Rest;
using RATSharp.Collection;
using RATSharp.Model;
using RATSharp;
using System.Threading.Tasks;
using RATSharp.Property;
using RATSharp.Cal;
using RATSharp.Validator;

namespace RATSharp
{
    class ObjectViewer
    {
        RestService service;
        RATSharp.Model.Object obj;
        Objects favourites;

        public ObjectViewer(RestService service, RATSharp.Model.Object obj)
        {
            this.obj = obj;
            this.service = service;
        }

        public void init()
        {
            invalidateContext();
            bool stepback = false;
            while (!stepback)
            {

                MenuList<MenuItem<string, MenuHandler>, MenuItem<string, MenuHandler>> header =
                      new MenuList<MenuItem<string, MenuHandler>, MenuItem<string, MenuHandler>>("Object " + obj.name);
                Console.WriteLine(header);
                Console.WriteLine("Object name: " + obj.name);
                Console.WriteLine("Description: " + obj.description);
                Console.WriteLine("Object Class: " + obj.objectClassName);
                MenuList<MenuItem<string, MenuHandler>, MenuItem<string, MenuHandler>> menuList =
          new MenuList<MenuItem<string, MenuHandler>, MenuItem<string, MenuHandler>>("");

                menuList.Add(new MenuItem<string, MenuHandler>("Object Calendar", new MenuHandler(handleObjectCalendar)));
                menuList.Add(new MenuItem<string, MenuHandler>(searchObjInFavourites(obj) ? "Unfavourite" : "Favourite", new MenuHandler(handleChangeFavourite)));
                menuList.Add(new MenuItem<string, MenuHandler>("Pick Up", new MenuHandler(handlePickUpAction)));
                menuList.Add(new MenuItem<string, MenuHandler>("Get Back", delegate() { stepback = true; }));
                Console.WriteLine(menuList);
                int choice = MenuUtilities.GetSimpleInput(new IntRangeInputValidator(1, menuList.fullCount)) - 1;
                menuList.ElementAt(choice).menuHandler();
            }
            
            }

        private void handlePickUpAction()
        {
            PickUpAction.pickUpAction(obj, service);
        }

        private void handleChangeFavourite()
        {
            Objects responseObj;
            if (!MenuUtilities.WaitForCommTask(searchObjInFavourites(obj) ? service.removeFavourite(obj.id) : service.addFavourite(obj.id), out responseObj))
            {
                return;
            }
            favourites = responseObj;
        }



        private void handleObjectCalendar()
        {
            Console.WriteLine("What year?");
            int year = MenuUtilities.GetSimpleInput(new IntRangeInputValidator(1970, 2500));
            Console.WriteLine("What month?");
            int month = MenuUtilities.GetSimpleInput(new IntRangeInputValidator(1, 12));


            AbstractCalendarDataProvider dataProvider = new CalendarObjectDataProvider(service, month, year, obj.id);
            Calendar calendar = new Calendar(year, month, dataProvider);
            calendar.reservationHandler = new ObjectReservationHandler(service, dataProvider.invalidate);

            calendar.init();
        }

        private bool searchObjInFavourites(RATSharp.Model.Object obj)
        {
            foreach (RATSharp.Model.Object favourite in favourites)
            {
                if (favourite.id == obj.id)
                {
                    return true;
                }
            }
            return false;
        }

        public void invalidateContext()
        {
           MenuUtilities.WaitForCommTask(service.getFavouritesAsync(), out favourites);
        }

    }
}
